package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.entity.EmployeeContracts;
import org.example.service.EmployeeContractsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/employeeContracts")
public class EmployeeContractsController {
    @Autowired
    private EmployeeContractsService employeeContractsService;
    @GetMapping("/list")
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<EmployeeContracts> page = new Page<>(pageNum, pageSize);
        Page<EmployeeContracts> result = employeeContractsService.page(page);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("data", result);
        response.put("message", "查询成功");

        return response;
    }

    @GetMapping("/detail/{id}")
    public Map<String, Object> detail(@PathVariable Integer id) {
        EmployeeContracts contract = employeeContractsService.getById(id);
        Map<String, Object> response = new HashMap<>();
        if (contract != null) {
            response.put("code", 200);
            response.put("data", contract);
            response.put("message", "查询成功");
        } else {
            response.put("code", 404);
            response.put("message", "未找到该合同");
        }

        return response;
    }

    @PutMapping("/update")
    public Map<String, Object> update(@RequestBody EmployeeContracts contract) {
        boolean result = employeeContractsService.updateById(contract);
        Map<String, Object> response = new HashMap<>();
        if (result) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 500);
            response.put("message", "更新失败");
        }
        return response;
    }

    @DeleteMapping("/delete")
    public Map<String, Object> delete(@RequestBody Map<String, Object> request) {
        try {
            List<Integer> ids = (List<Integer>) request.get("ids");

            if (ids == null || ids.isEmpty()) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 400);
                response.put("message", "请选择要删除的记录");
                return response;
            }

            boolean result = employeeContractsService.removeByIds(ids);
            Map<String, Object> response = new HashMap<>();
            if (result) {
                response.put("code", 200);
                response.put("message", "删除成功");
            } else {
                response.put("code", 500);
                response.put("message", "删除失败");
            }
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "删除失败: " + e.getMessage());
            return response;
        }
    }

    @DeleteMapping("/delete/{id}")
    public Map<String, Object> deleteById(@PathVariable Integer id) {
        try {
            if (id == null) {
                Map<String, Object> response = new HashMap<>();
                response.put("code", 400);
                response.put("message", "合同ID不能为空");
                return response;
            }
            boolean result = employeeContractsService.removeById(id);
            Map<String, Object> response = new HashMap<>();
            if (result) {
                response.put("code", 200);
                response.put("message", "删除成功");
            } else {
                response.put("code", 500);
                response.put("message", "删除失败，未找到该合同");
            }
            return response;
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "删除失败: " + e.getMessage());
            return response;
        }
    }
    @PostMapping("/add")
    public Map<String, Object> add(@RequestBody EmployeeContracts contract) {
        try {
            System.out.println("接收到的合同数据: " + contract);
            if (contract.getMainContract() == null) {
                contract.setMainContract("否");
            }
            boolean result = employeeContractsService.save(contract);

            Map<String, Object> response = new HashMap<>();
            if (result) {
                response.put("code", 200);
                response.put("message", "添加成功");
            } else {
                response.put("code", 500);
                response.put("message", "添加失败");
            }
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "添加失败: " + e.getMessage());
            return response;
        }
    }
}